# coding:utf-8
"""
description:scene 添加或修改的condition
"""
import os
import random

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML
from pages.Devices.TaskDevice_page import TaskDevice_page

Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'ConditionDev.yaml'))


class ConditionDevice_page(TaskDevice_page):
    by = mobileby.MobileBy()

    """点击timer一行，跳转页面"""
    ConditionDeviceTimer_loc = (by.ID, Data.get_elementInfo(0))
    """点击Schedule一行，跳转页面"""
    ConditionDeviceSchedule_loc = (by.ID, Data.get_elementInfo(1))

    """通用，除schedule外，condition device页面通用save按钮"""
    ConditionActionSave_loc = (by.ID, Data.get_elementInfo(24))

    def click_ConditionDeviceTimer(self):
        """点击timer一行，跳转页面"""
        self.find_element(*self.ConditionDeviceTimer_loc).click()

    def click_ConditionDeviceSchedule(self):
        """点击Schedule一行，跳转页面"""
        self.find_element(*self.ConditionDeviceSchedule_loc).click()

    def click_ConditionActionSave(self):
        """通用，除schedule外，condition device页面通用save按钮"""
        self.find_element(*self.ConditionActionSave_loc).click()

    """schedule，rule为one of，则schedule+other device，schedule没有作用"""
    ConditionStartTime_loc = (by.ID, Data.get_elementInfo(12))
    """chedule end time"""
    ConditionEndTime_loc = (by.ID, Data.get_elementInfo(13))
    """schedule页面的save按钮"""
    ConditionScheduleSave_loc = (by.ID, Data.get_elementInfo(14))

    def click_ConditionStartTime(self):
        """schedule，start time，rule为one of，则schedule+other device，schedule没有作用"""
        self.find_element(*self.ConditionStartTime_loc).click()

    def click_ConditionEndTime(self):
        """Shedule end time"""
        self.find_element(*self.ConditionEndTime_loc).click()

    def click_ConditionScheduleSave(self):
        """schedule页面的save按钮"""
        self.find_element(*self.ConditionScheduleSave_loc).click()

    def func_ConditionTimer(self, indexs=0, upAndDown="up", times=1, repeattimes=0):
        """
         保存timer
        :param indexs: number picker的索引号进行滑动
        :param upAndDown: 向上滑动还是向下滑动
        :param times: 滑动次数，点击repeat 日期次数
        :param repeattimes:
        :return:
        """
        super().swipe_NumberPicker_input(indexs, upAndDown, times)
        self.click_Repeat(repeattimes)
        self.click_ConditionActionSave()

    def func_ConditionSchedule(self, indexs=0, upAndDown="up", times=1, repeattimes=5):
        """
        保存Schedule,rule为one of，则schedule+other device，schedule没有作用
        :param Numindex: number picker的索引号进行滑动
        :param upAndDown: 向上滑动还是向下滑动
        :param times: 滑动次数，点击repeat 日期次数
        :return:
        """
        if self.assertVisible(*self.ConditionDeviceSchedule_loc):
            self.click_ConditionDeviceSchedule()
        self.click_ConditionStartTime()
        self.func_NumberPickerComfirm(indexs, upAndDown, times)
        # self.click_ConditionEndTime()
        # self.func_NumberPickerComfirm(indexs,upAndDown,times)
        self.click_Repeat(repeattimes)
        self.click_ConditionScheduleSave()

    def func_ConditionScheduleNUll(self):
        """
        不做更改直接save
        :return:
        """
        self.click_ConditionDeviceSchedule()
        self.click_ConditionScheduleSave()

    """1.condition列表页的device集合，点击跳转对应device操作详情页
    2.点击已添加的task device和已添加condition device"""
    ConditionDevices_loc = (by.ID, Data.get_elementInfo(2))
    """滑动Condition Device 控件"""
    rvAddConditionDevice_loc = (by.ID, Data.get_elementInfo(3))

    def click_ConditionDevices(self):
        """1.condition列表页的device集合，点击跳转对应device操作详情页
    2.点击已添加的task device和已添加condition device"""
        random.choice(self.find_elements(*self.ConditionDevices_loc)).click()

    def clickAndSwipe_ActionPercent(self):
        """向上滑动Condition Device 控件"""
        centre = gesture_mainpulation().centreXAndY(self.find_element(*self.rvAddConditionDevice_loc))
        self.driver.swipe(centre[0], centre[3], centre[0], centre[1] - 200, 200)

    """点击Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday后，
    分别显示once、weekends、 weekdays、everyday"""
    ScheduleRepeat_loc = (by.ID, Data.get_elementInfo(4))
    """Sunday"""
    Repeat1_loc = (by.ID, Data.get_elementInfo(5))
    """Monday"""
    Repeat2_loc = (by.ID, Data.get_elementInfo(6))
    """Tuesday"""
    Repeat3_loc = (by.ID, Data.get_elementInfo(7))
    """Wednesday"""
    Repeat4_loc = (by.ID, Data.get_elementInfo(8))
    """Thurday"""
    Repeat5_loc = (by.ID, Data.get_elementInfo(9))
    """Friday"""
    Repeat6_loc = (by.ID, Data.get_elementInfo(10))
    """Saturday"""
    Repeat7_loc = (by.ID, Data.get_elementInfo(11))

    def get_Repeat(self):
        """获取repeat选中的后文字显示"""
        return self.find_element(*self.ScheduleRepeat_loc).text

    def click_Repeat(self, times):
        """
        根据点击几次，来随机选择日期
        :param times:点击次数
        :return:
        """
        for i in range(times):
            nums = random.randint(1, 7)
            if nums == 1:
                LOG.info("随机选择日期，Sunday。。。")
                self.find_element(*self.Repeat1_loc).click()
            elif nums == 2:
                LOG.info("随机选择日期，Monday。。。")
                self.find_element(*self.Repeat2_loc).click()
            elif nums == 3:
                LOG.info("随机选择日期，Tuesday。。。")
                self.find_element(*self.Repeat3_loc).click()
            elif nums == 4:
                LOG.info("随机选择日期，Wednesday。。。")
                self.find_element(*self.Repeat4_loc).click()
            elif nums == 5:
                LOG.info("随机选择日期，Thurday。。。")
                self.find_element(*self.Repeat5_loc).click()
            elif nums == 6:
                LOG.info("随机选择日期，Friday。。。")
                self.find_element(*self.Repeat6_loc).click()
            else:
                LOG.info("随机选择日期，Saturday。。。")
                self.find_element(*self.Repeat7_loc).click()

    """station lite或station，temperature sensor，Humidity Sensor的数值"""
    ConditionStationValue_loc = (by.ID, Data.get_elementInfo(15))

    def input_ConditionStationValue(self, ConditionStationValue):
        """station lite或station，temperature sensor，Humidity Sensor的数值,限制3位数"""
        self.send_keys(ConditionStationValue, self.find_element(*self.ConditionStationValue_loc))

    def assertVisible_ConditionStationValue(self):
        """判断temperature和Humidity Sensor控件是否存在，存在则判定跳页为station temperature和Humidity Sensor"""
        return self.assertVisible(*self.ConditionStationValue_loc)

    def func_ConditionStationValue(self, ConditionStationValue):
        """
        temperature和Humidity Sensor
        :param ConditionStationValue:
        :return:
        """
        self.input_ConditionStationValue(ConditionStationValue)
        self.click_ConditionActionSave()

    """Motion sensor,Motion Detected"""
    PMSensor_loc = (by.ANDROID_UIAUTOMATOR, Data.get_elementInfo(16))

    def assert_PMSensor(self):
        """判断PM2.5 Sensor是否存在，存在则是station lite或station PM2.5 sensor页面"""
        return self.assertVisible(*self.PMSensor_loc)

    """Motion sensor,Motion Detected"""
    MotionDetected_loc = (by.ID, Data.get_elementInfo(17))
    """Motion sensor,No motion"""
    ConditionActionNoMotion_loc = (by.ID, Data.get_elementInfo(18))
    """door|window sensor open按钮"""
    ConditionActionOpen_loc = (by.ID, Data.get_elementInfo(19))
    """door|window sensor close按钮"""
    ConditionActionClose_loc = (by.ID, Data.get_elementInfo(20))

    def click_MotionDetected(self):
        """Motion sensor,Motion Detected"""
        self.find_element(*self.MotionDetected_loc).click()

    def assertVisible_MotionDetected(self):
        """判断Motion Detected按钮是否存在，存在则判定跳页为Motion sensor"""
        return self.assertVisible(*self.MotionDetected_loc)

    def click_ConditionActionNoMotion(self):
        """Motion sensor,No motion"""
        self.find_element(*self.ConditionActionNoMotion_loc).click()

    def click_ConditionActionOpen(self):
        """door|window sensor open按钮"""
        self.find_element(*self.ConditionActionOpen_loc).click()

    def assertVisible_ConditionActionOpen(self):
        """判断door|window Open按钮是否存在，存在则判定跳页为door|window sensor"""
        return self.assertVisible(*self.ConditionActionOpen_loc)

    def click_ConditionActionClose(self):
        """door|window sensor close按钮"""
        self.find_element(*self.ConditionActionClose_loc).click()

    """smoke、dimmer、1-3 gang switch、high power、plug、ceiling light，on按钮"""
    ConditionActionPowerOn_loc = (by.ID, Data.get_elementInfo(18))
    """smoke、dimmer、1-3 gang switch、high power、plug、ceiling light，off按钮"""
    ConditionActionPowerOff_loc = (by.ID, Data.get_elementInfo(19))

    """curtain和blind，device status，点击或者滑动控件，数值在0、25、50、75、100"""
    ConditionActionPercent_loc = (by.ID, Data.get_elementInfo(20))

    def click_ConditionActionPowerOn(self):
        """smoke、dimmer、1-3 gang switch、high power、plug、ceiling light，on按钮"""
        self.find_element(*self.ConditionActionPowerOn_loc).click()

    def assertVisible_ConditionActionPowerOn(self):
        """判断on按钮是否存在，存在则判定跳页为smoke、dimmer、1-3 gang switch、high power、
        plug、ceiling light页面"""
        return self.assertVisible(*self.ConditionActionPowerOn_loc)

    def click_ConditionActionPowerOff(self):
        """smoke、dimmer、1-3 gang switch、high power、plug、ceiling light，off按钮"""
        self.find_element(*self.ConditionActionPowerOff_loc).click()

    def clickswipe_ConditionActionPercent(self):
        """curtain和blind，device status，点击或者滑动控件，数值在0、25、50、75、100"""
        centre = gesture_mainpulation().centreXAndY(self.find_element(*self.ConditionActionPercent_loc))
        self.driver.swipe(centre[2], centre[1], centre[0] - 200, centre[1], 200)

    def assertVisible_ConditionActionPercent(self):
        """判断Action percent按钮是否存在，存在则判定跳页为curtain or blind"""
        return self.assertVisible(*self.ConditionActionPercent_loc)

    def func_ConditionDevice(self, ConditionStationValue="25", indexs=0, upAndDown="up",
                             times=1):
        """
        随机点击condition列表
        :param ConditionStationValue:
        :param indexs:
        :param upAndDown:
        :param times:
        :return:
        """
        LOG.info("随机点击condition列表，根据按钮控件判断所处condition device页面")

        if self.assertVisible_ConditionStationValue():
            LOG.info("判断station temperature和Humidity Sensor页面控件存在。。。%s" % self.assertVisible_ConditionStationValue())
            self.input_ConditionStationValue(ConditionStationValue)
            # self.swipe_NumberPicker_input(indexs,upAndDown,times)
        elif self.assert_PMSensor():
            LOG.info("判断station lite或station PM2.5 sensor页面控件存在。。。%s" % self.assert_PMSensor())
            self.func_NumberPickerComfirm(indexs, upAndDown, times)
        elif self.assertVisible_MotionDetected():
            LOG.info("判断Motion sensor页面Motion Detected按钮存在。。。%s" % self.assertVisible_MotionDetected())
            if random.randint(1, 2) == 1:
                self.click_MotionDetected()
            else:
                self.click_ConditionActionNoMotion()
        elif self.assertVisible_ConditionActionOpen():
            LOG.info("判断door|window sensor页面door|window Open按钮存在。。。%s" % self.assertVisible_ConditionActionOpen())
            if random.randint(1, 2) == 1:
                self.click_ConditionActionOpen()
            else:
                self.click_ConditionActionClose()
        elif self.assertVisible_ConditionActionPowerOn():
            LOG.info(
                "判断smoke、dimmer、1-3 gang switch、high power、plug、ceiling light页面存在on按钮。。。%s" % self.assertVisible_ConditionActionPowerOn())
            if random.randint(1, 2) == 1:
                self.click_ConditionActionPowerOn()
            else:
                self.click_ConditionActionPowerOff()
        elif self.assertVisible_ConditionActionPercent():
            LOG.info("判断curtain or blind页面存在Action percent控件。。。%s" % self.assertVisible_ConditionActionPercent())
            self.clickswipe_ConditionActionPercent()

        self.click_ConditionActionSave()
